package com.wdb007.venus.svc.biz.account;

import java.math.BigDecimal;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.biz.utils.DateFormatUtil;
import com.wdb007.venus.svc.biz.utils.DateUtil;
import com.wdb007.venus.svc.dal.user.dao.UserChargeLogDAO;
import com.wdb007.venus.svc.dal.user.model.UserChargeLogDO;
import com.wdb007.venus.svc.dal.vip.dao.VipAmountTblDAO;
import com.wdb007.venus.svc.dal.vip.dao.VipMemberDAO;
import com.wdb007.venus.svc.dal.vip.dao.VipMemberLogDAO;
import com.wdb007.venus.svc.dal.vip.model.VipMemberDO;
import com.wdb007.venus.svc.dal.vip.model.VipMemberLogDO;
import com.wdb007.venus.svc.facade.VipMemberService;
import com.wdb007.venus.svc.facade.WxAPPMessagePushService;
import com.wdb007.venus.svc.facade.account.model.VipMemberLogDTO;
import com.wdb007.venus.svc.facade.constant.BizConstants;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("vipMemberService")
public class VipMemberServiceImpl implements VipMemberService {

	private Logger logger = LoggerFactory.getLogger(VipMemberServiceImpl.class);
	
	@Autowired
	private VipMemberDAO vipMemberDAO;
	
	@Autowired
	private VipMemberLogDAO vipMemberLogDAO;
	
	@Autowired
	private VipAmountTblDAO vipAmountTblDAO;
	
	@Autowired
	private WxAPPMessagePushService wxAPPMessagePushService;
	
	@Autowired
	private UserChargeLogDAO userChargeLogDAO;
	
	@Override
	public String saveVipMember(VipMemberLogDTO vipMemberLogDTO) throws BizException {
		logger.info("保存vip信息:{}", JSON.toJSONString(vipMemberLogDTO));
		BigDecimal vipAmount = vipAmountTblDAO.getByVipType(vipMemberLogDTO.getVipType()).getVipAmount();
		vipMemberLogDTO.setVipAmount(vipAmount);
		VipMemberLogDO vipMemberLogDO = new VipMemberLogDO();
		vipMemberLogDO.setChargeType(vipMemberLogDTO.getChargeType());
		vipMemberLogDO.setUserId(vipMemberLogDTO.getUserId());
		vipMemberLogDO.setCreateTime(new Date());
		vipMemberLogDO.setVipType(vipMemberLogDTO.getVipType());
		vipMemberLogDO.setVipAmount(vipMemberLogDTO.getVipAmount());
		vipMemberLogDAO.save(vipMemberLogDO);
		
		VipMemberDO vipMember = new VipMemberDO();
		vipMember.setUserId(vipMemberLogDTO.getUserId());
		VipMemberDO result = vipMemberDAO.getByCondition(vipMember);
		Integer vipType = vipMemberLogDTO.getVipType();
		Date vipEndTime = null ;
		Integer chargeType = vipMemberLogDTO.getChargeType();
		if(null == result) {
			vipMember.setVipType(vipType);
			if(1 == vipType && BizConstants.VIP_CHARGE_TYPE_30.equals(chargeType)) {
				vipMember.setEndTime(DateFormatUtil.getSomeDay(new Date(), vipMemberLogDTO.getDays()));
			}else if(1== vipType) {
				vipMember.setRemainMonth(new BigDecimal("1.00"));
				vipMember.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextMouth(new Date())));
			}else if(2 == vipType) {
				vipMember.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(new Date())));
				vipMember.setRemainMonth(new BigDecimal("12.00"));
			}
			vipMember.setValidFlag(1);
			vipMember.setCreateTime(new Date());
			vipMember.setStartTime(new Date());
			vipMemberDAO.save(vipMember);
			vipEndTime = vipMember.getEndTime();
		}else {
			if(1 == vipType && BizConstants.VIP_CHARGE_TYPE_30.equals(chargeType)) {
				result.setEndTime(DateFormatUtil.getSomeDay(result.getEndTime(), vipMemberLogDTO.getDays()));
			}else if(1== vipType) {
				result.setVipType(vipType);
				result.setRemainMonth(result.getRemainMonth().add(new BigDecimal("1.00")));
				result.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextMouth(result.getEndTime())));
			}else if(2 == vipType) {
				result.setVipType(vipType);
				result.setEndTime(DateFormatUtil.getMaxTimeDay(DateUtil.getNextYear(result.getEndTime())));
				result.setRemainMonth(result.getRemainMonth().add(new BigDecimal("12.00")));
			}
			vipEndTime = result.getEndTime();
			result.setModifyTime(new Date());
			vipMemberDAO.update(result);
		}
		
		if(BizConstants.VIP_CHARGE_TYPE_10.equals(chargeType)) {
			UserChargeLogDO query = new UserChargeLogDO();
			query.setUserId(vipMemberLogDTO.getUserId());
			query.setVipType(vipMemberLogDTO.getVipType());
			vipAmount = userChargeLogDAO.getVipAmountByUserId(query);
		}else if(BizConstants.VIP_CHARGE_TYPE_30.equals(chargeType)) {
			vipAmount = new BigDecimal("0.00");
		}
		
		
		String vipEndTimeStr =  DateFormatUtil.formatDate2(vipEndTime);
		wxAPPMessagePushService.sendVipCardToAccountNotice(vipType, vipAmount, DateFormatUtil.formatDate2(new Date()), vipMemberLogDTO.getUserId(), vipEndTimeStr, vipMemberLogDTO.getChargeType());
		return vipEndTimeStr;
	}

}
